<?php

/**
 * Subclass for representing a row from the 'player' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Player extends BasePlayer
{
	public function addChallenge()	{
		$this->setNbChallenge($this->getNbChallenge()+1) ;
		return $this->getNbChallenge() ;
	}
	
	public function addWin()	{
		$this->setNbWin($this->getNbWin()+1) ;
		$this->addChallenge() ;
		return $this->getNbWin() ;
	}
	
	public function addLoss()	{
		$this->setNbLoss($this->getNbLoss()+1) ;
		$this->addChallenge() ;
		return $this->getNbLoss() ;
	}
	
	public function addDraw()	{
		$this->setNbDraw($this->getNbDraw()+1) ;
		$this->addChallenge() ;
		return $this->getNbDraw() ;
	}
	
	public function getRating()	{
		$n = $this->getNbChallenge() ;
		
		if ($n == 0)	{
			return 0 ;
		}

		$x = $this->getNbWin() + $this->getNbDraw() * 0.5 ;
		
		return $x / $n ;
	}
	
	public function getRank()	{
		$points = $this->getPoints() ;
		$points = $points ? $points : 0 ;
		
		$c = new Criteria() ;
	  $c->add(PlayerPeer::POINTS, $points, Criteria::GREATER_THAN) ;
		$rank = PlayerPeer::doCount($c) ;
		
		return $rank + 1 ;
	}
	
	public function getUsername()	{
		return $this->getsfGuardUser()->getUsername() ;
	}
}
